// const pageX = ref(0)
// const pageY = ref(0)

function banCopy(e: Event): void {
  let selection = window.getSelection()
  let selectedText = selection.toString()
  //添加判断条件，如果选中的文本长度大于50，则禁止复制
  if (selectedText.length > 50) {
    e.preventDefault()
    const rect = e.target.getBoundingClientRect()
    generateDiv(rect.left, rect.top + rect.height)
  }
}

// function mouseMove(e: Event): void {
//   pageX.value = e.pageX
//   pageY.value = e.pageY
// }

// 生成提示内容
const time = ref()

function generateDiv(x: Number, y: Number) {
  // console.log(x, y)
  if (time.value) {
    return
  }
  const div = document.createElement('div')
  div.style.position = 'absolute'
  div.style.top = y + 'px'
  div.style.left = x + 'px'
  div.style.zIndex = '9999'
  div.style.padding = '10px 15px'
  div.style.borderRadius = '5px'
  div.style.backgroundColor = '#999'
  div.style.color = 'white'
  div.className = 'generateDiv'
  div.innerHTML = '页面禁止复制超过50个字'
  document.body.appendChild(div)
  time.value = setTimeout(() => {
    deleteDiv()
    clearTimeout(time.value)
    time.value = ''
  }, 1500)
}

// 删除生成内容
function deleteDiv() {
  const div = document.querySelector('div.generateDiv')
  if (div) {
    document.body.removeChild(div)
  }
}

export function copyListener() {
  // document.addEventListener('mousemove', mouseMove)
  document.addEventListener('copy', banCopy)
}

export function removeCopy() {
  document.removeEventListener('copy', banCopy)
}
